<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2009, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Language Helpers
 *
 * @package		CodeIgniter
 * @subpackage	Helpers
 * @category	Helpers
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/helpers/language_helper.html
 */

// ------------------------------------------------------------------------

/**
 * Lang
 *
 * Fetches a language variable and optionally outputs a form label
 *
 * @access	public
 * @param	string	the language line
 * @param	string	the id of the form element
 * @return	string
 */	
if ( ! function_exists('lang')){
	function lang($line, $id = ''){
		$CI =& get_instance();
		$line = $CI->lang->line($line);
		if ($id != ''){
			$line = '<label for="'.$id.'">'.$line."</label>";
		}
		return $line;
	}
}
/**
 * get_content
 *
 * Get content object for current language
 * 
 * @author	locdo@penwoodtech.com
 * @access	public
 * @param	string	the table name
 * @param	string	the id of the parent element
 * @return	object
 */	
if ( ! function_exists('get_content')){
	function get_content($tbl,$id){
		$CI =& get_instance();	
		$l = get_cookie('language')?get_cookie('language'):$CI->config->item('language');
		$query = $CI->db->get_where($tbl.'_language',array('p_id'=>$id));		
		if($query->num_rows()>0){
			foreach($query->result() as $row){
      			if($row->l_code==$l) return $row;
			}
			return $query->row();
		}
		foreach($query->list_fields() as $field){
		   $obj->$field = '';
		}
		return $obj;
	}
}
/**
 * set_content
 *
 * Set content object for current language
 * 
 * @author	locdo@penwoodtech.com
 * @access	public
 * @param	string	the table name
 * @param	string	the id of the parent element
 * @param	array	new values with field=>value
 * @return	none
 */	
if ( ! function_exists('set_content')){
	function set_content($tbl,$id,$arr){
		$CI =& get_instance();	
		$l = get_cookie('language')?get_cookie('language'):$CI->config->item('language');		
		$query = $CI->db->get_where($tbl.'_language',array('p_id'=>$id,'l_code'=>$l));
		if($query->num_rows()>0){
			$CI->db->where(array('p_id'=>$id,'l_code'=>$l));
			$CI->db->update($tbl.'_language',$arr);
		}
		else{
			$CI->db->set($arr);
			$CI->db->set('p_id',$id);
			$CI->db->set('l_code',$l);			
			$CI->db->insert($tbl.'_language');
		}
	}
}
/**
 * delete_content
 *
 * Delete content object for all language
 * 
 * @author	locdo@penwoodtech.com
 * @access	public
 * @param	string	the table name
 * @param	string	the id of the parent element
 * @return	none
 */	
if ( ! function_exists('delete_content')){
	function delete_content($tbl,$id){
		$CI =& get_instance();		
		$CI->db->delete($tbl.'_language',array('p_id'=>$id)); 
	}
}
// ------------------------------------------------------------------------
/* End of file language_helper.php */
/* Location: ./system/helpers/language_helper.php */